package cn.edu.service.impl;

import cn.edu.entity.UserEntity;
import cn.edu.repository.UserRepository;
import cn.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserRepository userRepository;

    public UserEntity get(long id){
       return userRepository.get(id);
    }
    public UserEntity check(String username, String password) {
        List<UserEntity> all = userRepository.findAll();
        for (int i = 0; i < all.size(); i++) {
            UserEntity userEntity = all.get(i);
            if (userEntity.getName().equals(username)&&userEntity.getPassword().equals(password))
                return userEntity;
        }
        return null;
    }

    public void put(UserEntity userEntity) {
        userRepository.saveOrUpdate(userEntity);
    }

    public void delete(UserEntity userEntity) {
        userRepository.delete(userEntity.getId());
    }

    public void post(UserEntity userEntity) {
        userRepository.saveOrUpdate(userEntity);
    }

    public List<UserEntity> listUsers() {
        List<UserEntity> all = userRepository.findAll();
        return all;
    }
}
